package com.shenda;

import java.awt.SystemTray;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.shenda.starter.config.Settings;
import com.shenda.starter.operation.cmd.UpgraderCommand;
import com.shenda.starter.page.fx.BackupPage;
import com.shenda.starter.page.fx.MainApp;
import com.shenda.starter.page.fx.MainPage;
import com.shenda.starter.page.fx.SplashPage;
import com.swak.Application;
import com.swak.annotation.Server;
import com.swak.fx.support.AbstractApplication;
import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.Display;
import com.swak.reactivex.context.EndPoints.EndPoint;
import com.swak.ui.Event;

import javafx.application.Platform;
import javafx.scene.image.Image;
import javafx.stage.Stage;

/**
 * 测试启动
 * 
 * @author lifeng
 */
public class FxStarter extends AbstractApplication {

	/**
	 * 执行监听关闭
	 */
	@Override
	public void listen(Event event) {
		if (event == Event.EXIT) {
			this.stop(Display.getStage()).whenComplete((v, t) -> {
				Platform.exit();
				System.exit(0);
			});
		} else if (event == Event.UPGRADE) {
			this.stop(Display.getStage()).whenComplete((v, t) -> {
				new UpgraderCommand().exec();
				Platform.exit();
			});
		}
	}

	/**
	 * 启动服务
	 */
	@Override
	protected CompletableFuture<Void> start(String[] savedArgs) {
		return CompletableFuture.runAsync(() -> {
			Application.run(AppRunner.class, savedArgs);
		});
	}

	/**
	 * 停止服务
	 */
	@Override
	protected CompletableFuture<Void> stop(Stage stage) {
		CompletableFuture<Void> stopFuture = new CompletableFuture<>();
		CompletableFuture<Void> backupFuture = this.backup();
		CompletableFuture.runAsync(() -> {
			Application.stop();
		}).thenAcceptBothAsync(backupFuture, (v1, v2) -> {
			stopFuture.complete(null);
		});
		return stopFuture;
	}

	/**
	 * 启动
	 * 
	 * @param stage
	 * @return
	 */
	protected CompletableFuture<Void> backup() {
		CompletableFuture<Void> closeFuture = new CompletableFuture<>();
		BackupPage backupPage = new BackupPage();
		Display.runUI(() -> {
			Stage backupPageState = backupPage.openOn(null);
			this.customStage(backupPageState, null);
			backupPage.waitClose().whenComplete((v, t) -> {
				Display.runUI(() -> {
					backupPageState.close();
				});
				closeFuture.complete(null);
			});
		});
		return closeFuture;
	}

	/**
	 * 整个程序的定义
	 */
	@Override
	protected void customStage(Stage stage, SystemTray tray) {
		stage.setTitle("深大穿戴.陀螺仪");
		stage.getIcons().add(new Image(Display.load(MainApp.class, "logo.png").toExternalForm()));
		stage.sizeToScene();
		stage.setOnCloseRequest(event -> {
			Display.getEventBus().post(Event.CLOSE);
			event.consume();
		});
	}

	/**
	 * 发送要显示的地址
	 */
	@Override
	protected void postInitialized(AbstractPage mainPage) {
		List<EndPoint> endPoints = Application.getEndPoints().getEndPoints();
		for (EndPoint endPoint : endPoints) {
			if (endPoint.getScheme() == Server.Http) {
				String server = endPoint.toString();
				Display.getEventBus().post(Event.URL.message(server));
				Settings.me().setServer(server);
			}
		}
	}

	/**
	 * 启动系统
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Settings.intSettings();
		Platform.setImplicitExit(false);
		launch(FxStarter.class, MainPage.class, SplashPage.class, args);
	}
}
